package wang.luo.clnczjl;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.afollestad.materialdialogs.LayoutMode;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.bottomsheets.BottomSheet;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.StringUtils;

public class setCnsActivity extends AppCompatActivity {
    private Button button;
    private EditText cnsIpEditText;
    private EditText cnsPwEditText;
    private CheckBox checkBox;
    private boolean cnsStatus = false;
    private String cnsIp = null;
    private String cnsPw = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_cns);

        button = findViewById(R.id.button);
        cnsIpEditText = findViewById(R.id.cnsIpEditText);
        cnsPwEditText = findViewById(R.id.cnsPwEditText);
        checkBox = findViewById(R.id.checkBox);

        SharedPreferences cnsInfo = getSharedPreferences(MainActivity.PREFS_CNS, MODE_PRIVATE);

        cnsStatus = cnsInfo.getBoolean("status", false);
        cnsIp = cnsInfo.getString("myCnsIp", null);
        cnsPw = cnsInfo.getString("myCnsPw", null);

        Log.d("cnsStatus",""+cnsStatus);

        cnsIpEditText.setText(cnsIp);
        cnsPwEditText.setText(cnsPw);
        checkBox.setChecked(cnsStatus);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                cnsStatus = isChecked;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cnsIp = cnsIpEditText.getText().toString();
                cnsPw = cnsPwEditText.getText().toString();
//                if (cnsStatus && !StringUtils.isTrimEmpty(cnsIp) && !StringUtils.isTrimEmpty(cnsPw)) {
                    dialog();
//                }
            }
        });
    }

    private void dialog() {
        BottomSheet bottomSheet = new BottomSheet(LayoutMode.WRAP_CONTENT);
        MaterialDialog dialog = new MaterialDialog(setCnsActivity.this, bottomSheet);
        dialog.title(null, "提示：");
        dialog.message(null, "修改后需要重启APP并再次安装脚本才会生效！", null);
        dialog.positiveButton(null, "确认", materialDialog -> {
            MainActivity.myCnsIp = cnsIp;
            MainActivity.myCnsPw = cnsPw;
            MainActivity.customCnsEn = cnsStatus;
            SharedPreferences cnsInfo = getSharedPreferences(MainActivity.PREFS_CNS, MODE_PRIVATE);
            @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = cnsInfo.edit();//获取Editor
            editor.putBoolean("status", MainActivity.customCnsEn);
            editor.putString("myCnsIp", MainActivity.myCnsIp);
            editor.putString("myCnsPw", MainActivity.myCnsPw);
            editor.apply();
//            finish();
            AppUtils.relaunchApp();
            return null;
        });
        dialog.negativeButton(null, "取消", materialDialog -> {
            return null;
        });
        dialog.show();
    }
}